<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'
import {exportHandle} from '@/utils'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      stepsConfig: {
        steps: [
          {
            title: '步骤1',
            Attrs: ['orderDetailId', 'productId', 'deviceId', 'modelId', 'clientProductNo', 'boxId', ' orderProductNumber', 'groupNumber'],
            description: ''
          },
          {
            title: '步骤2',
            Attrs: ['qualityGrade', 'bottleCapSuit', 'deepTypeList', 'customerProductStyle', 'packRequire', 'customerRequire', 'bottleHeight', 'bottleInDiameter', 'headNeckHeight', 'bottleOutDiameter', 'productWeight','weightScope','volumeScope'],
            description: ''
          },
          {title: '步骤3', description: ''}
        ]
      },
      dataFormConfig: {
        id: {
          hidden: true
        },
        orderDetailId: {
          label: '订单',
          type: 'list',
          selectCallback: this.orderSelectCallback,
          list: [],
          placeholder: '请输入订单'
        },
        productId: {
          label: '产品',
          type: 'list',
          list: [],
          placeholder: '请选择产品'
        },
        productInStorage: {
          label: '库存数量',
          disabled: true,
          hidden: true
        },
        modelId: {
          label: '模具编号',
          type: 'list',
          list: [],
          placeholder: '请选择模具编号'
        },
        clientProductNo: {
          label: '客户产品编号',
          placeholder: '请输入客户产品编号'
        },
        deviceId: {
          label: '设备',
          list: [],
          type: 'list',
          placeholder: '请选择设备'
        },
        qualityGrade: {
          label: '质量等级',
          type: 'radio',
          default: 0,
          radioList: [
            {label: 0, text: 'A'},
            {label: 1, text: 'B'}
          ]
        },
        bottleCapSuit: {
          label: '瓶盖配套',
          type: 'switch',
          default: 0
        },
        customerProductStyle: {
          label: '定制化要求',
          type: 'switch',
          default: 0
        },
        packRequire: {
          label: '包装要求',
          placeholder: '请输入包装要求',
          reply: 'customerProductStyle'
        },
        customerRequire: {
          label: '客户质量要求',
          placeholder: '请输入客户质量要求',
          reply: 'customerProductStyle'
        },
        boxId: {
          label: '纸箱',
          type: 'list',
          placeholder: '请选择纸箱',
          list: []
        },
        bottleHeight: {
          label: '瓶身总高度',
          type: 'number'
        },
        bottleInDiameter: {
          label: '瓶口内径',
          type: 'number'
        },
        headNeckHeight: {
          label: '头颈高度',
          type: 'number'
        },
        bottleOutDiameter: {
          label: '瓶口外径',
          type: 'number'
        },
        productWeight: {
          label: '产品重量',
          type: 'number'
        },
        weightScope: {
          label: '重量误差(±)',
          type: 'String'
        },
        // productVolume: {
        //   label: '产品容量',
        //   type: 'number'
        // },
        volumeScope:{
          label: '容量范围(±)',
          type: 'String'
        },
        orderProductNumber: {
          label: '订单数量',
          type: 'number',
          max: 10000000

        },
        groupNumber: {
          label: '组数',
          type: 'number',
          max: 1000,
          default: 0
        },
        classesInfoId: {
          label: '换产班组',
          list: [],
          type: 'list',
          placeholder: '请选择组别'
        },
        nextProductId: {
          label: '换产产品编号',
          type: 'list',
          list: [],
          placeholder: '请选择产品'
        },
        nextProductNumber: {
          label: '换产数量',
          type: 'number'
        },
        dayTime: {
          label: '日期',
          type: 'date',
          placeholder: '请输入日期'
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    orderSelectCallback(value) {
      mRequest('orderApi/orderDetailInfo', {id: value}, (res) => {
        const orderDetail = this.R.pathOr({}, ['data', 'orderDetail'], res)

        this.dataForm['bottleCapSuit'] = orderDetail['bottleCapSuit']
        this.dataForm['bottleHeight'] = orderDetail['bottleHeight']
        this.dataForm['bottleInDiameter'] = orderDetail['bottleInDiameter']
        this.dataForm['bottleOutDiameter'] = orderDetail['bottleOutDiameter']
        this.dataForm['customerProductStyle'] = orderDetail['customerProductStyle']
        this.dataForm['customerRequire'] = orderDetail['customerRequire']
        this.dataForm['headNeckHeight'] = orderDetail['headNeckHeight']
        this.dataForm['deviceId'] = orderDetail['deviceId']
        // this.dataForm['packRequire'] = orderDetail['packRequire']
        this.dataForm['productId'] = orderDetail['productId']
        this.dataForm['productWeight'] = orderDetail['productWeight']
        this.dataForm['remark'] = orderDetail['remark']
        this.dataForm['orderProductNumber'] = orderDetail['orderProductNumber']
        this.dataForm['volumeScope'] = orderDetail['volumeScope']
        this.dataForm['weightScope'] = orderDetail['weightScope']

        mRequest('productApi/productInfoInfo', {id: this.dataForm['productId']}, (productRes) => {
          const productInfo = this.R.pathOr({}, ['data', 'productInfo'], productRes)

          this.dataForm['modelId'] = productInfo['modelId']
        })
      })
    },
    submitCallback(cb) {
      return (value) => {
        if (this.R.equals(0, this.dataForm['id'])) {
          this.$confirm('是否直接导出改计划?', '提示', {
            confirmButtonText: '导出',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            exportHandle('productApi/getProductPlanNoticeExport', {productPlanNoticeId: value['id']})
          }).catch(() => {
          })
          this.$emit('refreshDataList')
        }
        if (this.R.isNil(cb) && cb instanceof Function) {
          return cb()
        }
      }
    },
    init(id, property, cb) {
      this.dataForm.id = id || 0
      this.initProductDeviceList()
      this.initOrderDetailList()
      this.initProductInfoList()
      this.initModelInfoList('modelId')
      this.initClassInfoList('classesInfoId')
      this.initProductInfoList('nextProductId')
      this.initBoxInfoIdList('boxId')

      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        stepsConfig: this.stepsConfig,
        dataRule: this.getNeedDataRule('orderDetailId', 'deviceId','dayTime', 'groupNumber', 'status'),
        initApi: 'productApi/productPlanNoticeInfo',
        saveApi: 'productApi/saveProductPlanNotice',
        updateApi: 'productApi/updateProductPlanNotice',
        objectName: 'productPlanNotice',
        submitCallback: this.submitCallback(cb)
      })

      this.$nextTick(() => {
        if (!this.R.isNil(property)) {
          Object.keys(property).forEach((key) => {
            this.dataForm[key] = property[key]
          })
          this.orderSelectCallback(this.dataForm['orderDetailId'])
        }
      })

    }
  }
}
</script>
